import 'dart:io';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';

class FileUtil {
  ///保存文件
  static Future<String> save2File(Uint8List bytes, String fileName, {String dir = 'source'}) async {
    // 获取应用的临时目录
    final directory = await getApplicationDocumentsDirectory();
    final Directory d = Directory('${directory.path}/$dir/');
    d.createSync(recursive: true);
    final filePath = '${directory.path}/$dir/$fileName';
    await File(filePath).writeAsBytes(bytes);
    return filePath;
  }

  ///读取文件
  static Uint8List readFile(String filePath) {
    final file = File(filePath);
    return file.readAsBytesSync();
  }

  ///判断文件是否存在
  static Future<bool> isFileExist(String fileName, {String dir = 'source'}) async {
    // 获取应用的临时目录
    final directory = await getApplicationDocumentsDirectory();
    final filePath = '${directory.path}/$dir/$fileName';

    final file = File(filePath);
    return file.existsSync();
  }

  static Future<String> getFilePath(String fileName, {String dir = 'source'}) async {
    // 获取应用的临时目录
    final directory = await getApplicationDocumentsDirectory();
    return '${directory.path}/$dir/$fileName';
  }
}
